package com.woniuxy.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.woniuxy.dao.FocusDao;
import com.woniuxy.dao.OrdersDao;
import com.woniuxy.dao.RemarkDao;
import com.woniuxy.entity.Orders;
import com.woniuxy.entity.PageBean;
import com.woniuxy.entity.User;
import com.woniuxy.service.OrdersService;
import com.woniuxy.util.CommonUtil;
/**
 * 
 * @author lishuwei&曾令忠
 *
 */
@Service
public class OrdersServiceImpl implements OrdersService {
	@Autowired
	private OrdersDao ordersDao;
	@Autowired
	private FocusDao focusDao;
	@Autowired
	private RemarkDao remarkDao;
	
	/**
	 * @author 曾令忠
	 * 分页获取教练的学员
	 * id:教练id
	 */
	@Override
	public PageBean<User> getCoachStusByPage(long id, int page) {
		PageBean<User> pb = new PageBean<>();
		int pagesize = 5;
		int totalNum = ordersDao.getstusNum(id, "2","3");
		List<User> users = ordersDao.getStus(id, "2","3", (page-1)*pagesize, pagesize);
		users = CommonUtil.userSetInfo(id, users, focusDao);
		if(users==null||users.size()==0) {
			return pb;	
		}
		pb.setList(users);
		pb.setNowNum(users.size());
		pb.setPageSize(pagesize);
		pb.setTotalNum(totalNum);
		pb.setTotalPage(totalNum % pagesize == 0 ? totalNum / pagesize : totalNum / pagesize + 1);
		pb.setNowPage(page);
		return pb;
	}
	
	/**
	 * @author 曾令忠
	 * 分页获取教练的订单
	 * id:教练id；page:页码
	 */
	@Override
	public PageBean<Orders> getOrdersByCidByPage(long id, int page) {
		PageBean<Orders> pegeBean = new PageBean<>();
		int pagesize = 5;
		int totalNum = ordersDao.getOrdersNumByCid(id);
		List<Orders> Orders = ordersDao.getOrdersByCidByPage(id, (page-1)*pagesize, pagesize);
	
		if(Orders==null||Orders.size()==0) {
			return pegeBean;	
		}
		pegeBean.setList(Orders);
		pegeBean.setNowNum(Orders.size());
		pegeBean.setPageSize(pagesize);
		pegeBean.setTotalNum(totalNum);
		pegeBean.setTotalPage(totalNum % pagesize == 0 ? totalNum / pagesize : totalNum / pagesize + 1);
		pegeBean.setNowPage(page);
		return pegeBean;
	}

	/**
	 * @author 曾令忠
	 * 分页获取教练的订单
	 * id:学生id；page:页码
	 */
	@Override
	public PageBean<Orders> getOrdersBySidByPage(long id, int page) {
		PageBean<Orders> pegeBean = new PageBean<>();
		int pagesize = 5;
		int totalNum = ordersDao.getOrdersNumBySid(id);
		List<Orders> Orders = ordersDao.getOrdersBySidByPage(id, (page-1)*pagesize, pagesize);
	
		if(Orders==null||Orders.size()==0) {
			return pegeBean;	
		}
		pegeBean.setList(Orders);
		pegeBean.setNowNum(Orders.size());
		pegeBean.setPageSize(pagesize);
		pegeBean.setTotalNum(totalNum);
		pegeBean.setTotalPage(totalNum % pagesize == 0 ? totalNum / pagesize : totalNum / pagesize + 1);
		pegeBean.setNowPage(page);
		return pegeBean;
	}

	@Override
	public int getOrderNumById(long id, boolean status) {
		if (status) {
			return ordersDao.getNumByUserIdAndStatus(id,"3");
		}else if (!status) {
			return ordersDao.getNumByUserIdAndStatus(id,"4");
		}
		return 0;
	}

	@Override
	public int getStudentMarkNum(Integer id, int mark) {
		
		return remarkDao.getNumBySIdAndMark(id,mark);
	}

	@Override
	public int getCoachMarkNum(Integer id, int mark) {
		// TODO Auto-generated method stub
		return  remarkDao.getNumByCIdAndMark(id,mark);
	}

	@Override
	public Orders getOrdersByOid(long oid) {
		// TODO Auto-generated method stub
		return ordersDao.getOrdersByOid(oid);
	}

	@Override
	public void releaseCourse(long chooseId) {
		ordersDao.releaseCourse(chooseId);
		
	}

}
